package com.huluobo.a2110mvpimdemo.contact;


import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.huluobo.a2110mvpimdemo.R;
import com.huluobo.a2110mvpimdemo.adapter.ContactAdapter;
import com.huluobo.a2110mvpimdemo.base.BaseFragment;
import com.huluobo.a2110mvpimdemo.demo.SlideBar;
import com.hyphenate.EMContactListener;
import com.hyphenate.chat.EMClient;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

public class MyContactFragment extends BaseFragment<ContactContract.View, ContactPresenter> implements ContactContract.View {

    private RecyclerView recyclerView;
    private List<ContactListItemBean> listItemBeans = new ArrayList<>();
    private ContactAdapter contactAdapter;

    private SlideBar slideBar;

    private TextView contact_section;//用户展示字母

    private Button searchBtn;
    private EditText searchEt;

    @Override
    protected int bindLayoutId() {
        return R.layout.fragment_my_contact;
    }

    @Override
    protected void initData() {
        presenter = new ContactPresenter();
        presenter.loadContacts();
        //给好友管理器添加一个联系人的监听器,可以监听到好友列表的变化
        EMClient.getInstance().contactManager().setContactListener(new EMContactListener() {
            @Override
            public void onContactAdded(String username) {

            }

            @Override
            public void onContactDeleted(String username) {
                //监听到好友被删除,需要重新获取一下好友列表
                presenter.loadContacts();
            }

            @Override
            public void onContactInvited(String username, String reason) {

            }

            @Override
            public void onFriendRequestAccepted(String username) {

            }

            @Override
            public void onFriendRequestDeclined(String username) {

            }
        });
    }

    @Override
    protected void initView() {
        recyclerView = findViewById(R.id.contact_rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        contactAdapter = new ContactAdapter(listItemBeans, getActivity());
        recyclerView.setAdapter(contactAdapter);

        slideBar = findViewById(R.id.contact_slideBar);
        contact_section = findViewById(R.id.contact_section);
        slideBar.setOnSectionChangeListener(new SlideBar.OnSectionChangeListener() {
            @Override
            public void onSectionChange(String firstLetter) {
                contact_section.setVisibility(View.VISIBLE);
                contact_section.setText(firstLetter);
                moveToLetterPosition(firstLetter);
            }

            @Override
            public void onSlideFinish() {
                contact_section.setVisibility(View.GONE);
            }
        });

        searchBtn = findViewById(R.id.contact_btn_search);
        searchEt = findViewById(R.id.contact_ed_search);
        searchBtn.setOnClickListener(v -> {
            if (searchEt.getText().toString().length() > 0) {
                presenter.findContact(searchEt.getText().toString());
            }else {
                presenter.loadContacts();
            }
        });

    }

    //移动到首字符对应的位置
    private void moveToLetterPosition(String firstLetter) {
        for (int i = 0; i < listItemBeans.size(); i++) {
            //对比首字符是否相等
            if (String.valueOf(listItemBeans.get(i).getFirstLetter()).equals(firstLetter)) {
//                recyclerView.scrollToPosition(i);
                recyclerView.smoothScrollToPosition(i);//平滑移动到对应的position位置
            }
        }
    }

    @Override
    public void onLoadContactSuccess(List<ContactListItemBean> contactListItemBeans) {
        Log.i("TagA", "contactListItemBeans:" + contactListItemBeans);
        getActivity().runOnUiThread(() -> {
            listItemBeans.clear();
            listItemBeans.addAll(contactListItemBeans);
            contactAdapter.notifyDataSetChanged();
        });
    }

    @Override
    public void onLoadContactFailed(String error) {
        Log.i("TagA", "error:" + error);
    }

    @Override
    public void onFindContactSuccess(List<ContactListItemBean> contactListItemBeans) {
        getActivity().runOnUiThread(() -> {
            contactAdapter = new ContactAdapter(contactListItemBeans, getActivity());
            recyclerView.setAdapter(contactAdapter);
        });
    }

    @Override
    public void onFindContactFailed(String error) {
        getActivity().runOnUiThread(() -> {
            Toast.makeText(getActivity(), "搜索错误:" + error, Toast.LENGTH_SHORT).show();
        });
    }
}